import request from '../../utils/request';

// 商机信息接口
export interface BusinessVO {
  id?: number;
  name: string;
  customerId: number;
  customerName?: string;
  contactId?: number;
  contactName?: string;
  statusId: number;
  statusName?: string;
  endStatus?: number;
  endRemark?: string;
  ownerUserId: number;
  ownerUserName?: string;
  roUserIds?: string;
  rwUserIds?: string;
  price?: number;
  discountPercent?: number;
  productPrice?: number;
  remark?: string;
  dealTime?: Date;
  createTime?: Date;
  updateTime?: Date;
}

// 商机状态接口
export interface BusinessStatusVO {
  id?: number;
  name: string;
  typeId: number;
  sort: number;
  percent: number;
}

// 商机分页查询参数
export interface BusinessPageParams {
  pageNo?: number;
  pageSize?: number;
  name?: string;
  customerId?: number;
  statusId?: number;
  ownerUserId?: number;
  sceneType?: number;
}

// 商机 API
export const BusinessApi = {
  // 查询商机分页
  getBusinessPage: async (params: BusinessPageParams) => {
    return await request.get('/crm/business/page', { params });
  },

  // 查询商机详情
  getBusiness: async (id: number) => {
    return await request.get(`/crm/business/${id}`);
  },

  // 新增商机
  createBusiness: async (data: BusinessVO) => {
    return await request.post('/crm/business', data);
  },

  // 修改商机
  updateBusiness: async (data: BusinessVO) => {
    return await request.put('/crm/business', data);
  },

  // 删除商机
  deleteBusiness: async (id: number) => {
    return await request.delete(`/crm/business/${id}`);
  },

  // 更新商机状态
  updateBusinessStatus: async (id: number, statusId: number, endRemark?: string) => {
    return await request.put(`/crm/business/${id}/status`, { statusId, endRemark });
  },

  // 转移商机
  transferBusiness: async (ids: number[], newOwnerUserId: number) => {
    return await request.put('/crm/business/transfer', { ids, newOwnerUserId });
  },

  // 获取商机简单列表
  getBusinessSimpleList: async () => {
    return await request.get('/crm/business/simple-list');
  },

  // 导出商机
  exportBusiness: async (params: BusinessPageParams) => {
    return await request.get('/crm/business/export', { params, responseType: 'blob' });
  }
};

// 商机状态 API
export const BusinessStatusApi = {
  // 查询商机状态列表
  getBusinessStatusList: async (typeId?: number) => {
    return await request.get('/crm/business-status/list', { params: { typeId } });
  },

  // 查询商机状态详情
  getBusinessStatus: async (id: number) => {
    return await request.get(`/crm/business-status/${id}`);
  },

  // 新增商机状态
  createBusinessStatus: async (data: BusinessStatusVO) => {
    return await request.post('/crm/business-status', data);
  },

  // 修改商机状态
  updateBusinessStatus: async (data: BusinessStatusVO) => {
    return await request.put('/crm/business-status', data);
  },

  // 删除商机状态
  deleteBusinessStatus: async (id: number) => {
    return await request.delete(`/crm/business-status/${id}`);
  }
};